import {Component, OnInit} from "@angular/core";
import {NzModalService, NzModalSubject, NzNotificationService} from "ng-zorro-antd";
import {Router} from "@angular/router";
import {UnconfirmedMaintenanceBean} from "../bean/unconfirmedMaintenance.bean";
import {Config} from "../../../../service/config";
import {HttpServcie} from "../../../../service/http.service";
import {elevatorTypes} from "../../../../service/constant/elevatorTypeConstant";

@Component({
  selector: "needconfirm-maintenance",
  templateUrl: "./needconfirm-maintenance.html",
  styleUrls: ["./needconfirm-maintenance.scss"]
})
/**
 * 未确认维保单
 */
export class NeedconfirmMaintenance implements OnInit {
  // 实体类
  public unconfirmedMaintenance = new UnconfirmedMaintenanceBean();
  // 遮罩
  public nzSpinning: boolean = false;
  // 机构Id
  public partyId = JSON.parse(window.localStorage["user"]).partyId;
  // 机构类型
  public partyType = JSON.parse(window.localStorage["user"]).partyType;
  // 登录名
  public safeUserId = JSON.parse(window.localStorage["user"]).id;

  // 角色ID
  public roleId = JSON.parse(window.localStorage["user"]).roleId;

  // 返回数据
  public list: any = [];

  elevatorTypes = elevatorTypes;

  public flag: boolean = true;
  data: any;


  modal: any = 0;
  currentModal: NzModalSubject;
  modelTitle:"修改";
  updateData: any = [];
  updateList: any = [];
  total: string;
  config: any = Config;

  //多选框
  allChecked: any = false;
  indeterminate: any = false;



  /*初始化*/
  ngOnInit() {

  }
  /*构造方法*/
  constructor(
    private httpServcie: HttpServcie,
    private router: Router,
    private modalService: NzModalService,
    private _notification: NzNotificationService,
  ) {
    this.unconfirmedMaintenance.pageSize = 20;
    this.unconfirmedMaintenance.currentPage = 1;
    this.queryUp();
  }
  /**
   * 查询时调用,初始化调用
   */
  queryUp() {
    let param = {
      pageSize: this.unconfirmedMaintenance.pageSize,
      currentPage: this.unconfirmedMaintenance.currentPage,
      elevatorNum: this.unconfirmedMaintenance.elevatorNum,//外部关联码或设备代码
      useUnitId: this.partyId,  // 使用单位id
      outsideNum:this.unconfirmedMaintenance.outsideNum,
      safeUserId:this.safeUserId,
    }
    this.nzSpinning = true;
    this.httpServcie.queryNeedconfirmedMaintenance(param).subscribe(
      data => {
        if (data.status == 200) {
          this.nzSpinning = false;
          this.list = data.data.list;
          this.total = data.data.total;
        } else if (data.status === 401) {
          this.router.navigateByUrl("/login");
          return;
        } else {
          // 异常
          this._notification.create("error", "错误提示", data.message);
        }
      },
      error => {
        console.log(JSON.stringify(error));
      }
    );
  }

  /**
   * 确认未确认工单
   */
  sendNotice() {
    let sendList: any=[];
    const thiss: any = this;
    // 确认勾选上的数据
    for (let i = 0; i < this.list.length; i++) {
      if (this.list[i].checked) {
        sendList.push(this.list[i]);
      }
    }
    if (this.list.length == 0) {
      // 异常
      this._notification.create("warning", "警告提示", "您未选择数据，请至少选中一条数据！");
      return;
    }

    this.modalService.confirm({
      title: "您确认要确定这 " + sendList.length + " 条维保工单么？",
      content: "<b style='color: red'>确认后，维保工单会自动签上您的姓名！</b>",
      onOk() {
        let ob = thiss.httpServcie.sureUnconfirmed({"sendList":sendList,"userId":thiss.safeUserId});
        ob.subscribe(
          data => {
            if (data.status == 200) {
              thiss.queryUp();
            } else {
              thiss._notification.create("error", "错误提示", data.message);
            }
          },
          error => {
            thiss._notification.create("error", "错误提示", "发送失败！");
          }
        );
      },
      onCancel() {
      }
    });

  }

  // 添加后界面刷新
  addOutPut(event) {
    setTimeout(() => {
      this.currentModal.destroy();
      this.currentModal = null;
    }, 200);
    this.queryUp();
  }


  //全选
  checkAll(value) {
    if (value) {
      this.list.forEach(data => {
        if (!data.disabled) {
          data.checked = true;
        }
      });
    } else {
      this.list.forEach(data => (data.checked = false));
    }
  }

  //分页
  nzPageSizeChange(event){
    this.unconfirmedMaintenance.pageSize = event;
    this.unconfirmedMaintenance.currentPage = 1;
    this.queryUp();
  }

  //输入框的方法
  nzPageIndexChange(event) {
    this.unconfirmedMaintenance.currentPage = event;
    this.queryUp();
  }

  //当前页
  nzPageIndexClickChange(event) {
    this.unconfirmedMaintenance.currentPage = event;
    this.queryUp();
  }
}
